using System;
using System.Collections.Generic;
using System.Text;
using Csla.Data;
using Csla;
using Active.Core.DB;

namespace Active.Core
{
    [Serializable()]
    public class Contact : BusinessBase<Contact>
    {
        #region Business Methods

        private int _id;
        /// <summary>
        /// Gets the id.
        /// </summary>
        /// <value>The id.</value>
        public int Id
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _id;
            }
        }

        string _name = string.Empty;
        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get
            {
                CanReadProperty(true);
                return _name;
            }
            set
            {
                CanWriteProperty(true);
                if (_name != value)
                {
                    _name = value;
                    PropertyHasChanged();
                }
            }
        }

        int _companyId;
        /// <summary>
        /// Gets or sets the company id.
        /// </summary>
        /// <value>The company id.</value>
        public int CompanyId
        {
            get
            {
                CanReadProperty(true);
                return _companyId;
            }
        }

        string _Tel = string.Empty;
        /// <summary>
        /// Gets or sets the tel.
        /// </summary>
        /// <value>The tel.</value>
        public string Tel
        {
            get
            {
                CanReadProperty(true);
                return _Tel;
            }
            set
            {
                CanWriteProperty(true);
                if (_Tel!=value)
                {
                    _Tel = value;
                    PropertyHasChanged();
                }
            }
        }

        string _mobile = string.Empty;
        public string Mobile
        {
            get
            {
                CanReadProperty(true);
                return _mobile;
            }
            set
            {
                CanWriteProperty(true);
                if (_mobile!=value)
                {
                    _mobile = value;
                    PropertyHasChanged();
                }
            }
        }

        string _position = string.Empty;
        /// <summary>
        /// Gets or sets the position.
        /// </summary>
        /// <value>The position.</value>
        public string Position
        {
            get
            {
                CanReadProperty(true);
                return _position;
            }
            set
            {
                CanWriteProperty(true);
                if (_position!=value)
                {
                    _position = value;
                    PropertyHasChanged();
                }
            }
        }

        string _QQ = string.Empty;
        /// <summary>
        /// Gets or sets the QQ.
        /// </summary>
        /// <value>The QQ.</value>
        public string QQ
        {
            get
            {
                CanReadProperty(true);
                return _QQ;
            }
            set
            {
                CanWriteProperty(true);
                if (_QQ!=value)
                {
                    _QQ = value;
                    PropertyHasChanged();
                }
            }
        }

        string _MSN = string.Empty;
        /// <summary>
        /// Gets or sets the MSN.
        /// </summary>
        /// <value>The MSN.</value>
        public string MSN
        {
            get
            {
                CanReadProperty(true);
                return _MSN;
            }
            set
            {
                CanWriteProperty(true);
                if (_MSN!=value)
                {
                    _MSN = value;
                    PropertyHasChanged();
                }
            }
        }

        string _description = string.Empty;
        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>The description.</value>
        public string Description
        {
            get
            {
                CanReadProperty(true);
                return _description;
            }
            set
            {
                CanWriteProperty(true);
                if (_description!=value)
                {
                    _description = value;
                    PropertyHasChanged();
                }
            }
        }

        protected override object GetIdValue()
        {
            return _id;
        }

        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            //ValidationRules.AddRule(null, "");
        }

        #endregion

        #region Authorization Rules

        protected override void AddAuthorizationRules()
        {
            //AuthorizationRules.AllowWrite("", "");
        }

        /// <summary>
        /// Determines whether this instance [can add object].
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if this instance [can add object]; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanAddObject()
        {
            return Utils.IsMediaEmployee || Utils.IsMediaManager;
        }

        /// <summary>
        /// Determines whether this instance [can get object].
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if this instance [can get object]; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanGetObject()
        {
            return true;
        }

        /// <summary>
        /// Determines whether this instance [can edit object].
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if this instance [can edit object]; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanEditObject()
        {
            return true;
        }

        /// <summary>
        /// Determines whether this instance [can delete object].
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if this instance [can delete object]; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanDeleteObject()
        {
            return Utils.IsMediaManager;
        }

        #endregion

        #region Factory Methods

        internal static Contact NewContact()
        {
            return new Contact();
        }

        internal static Contact GetContact(SafeDataReader dr)
        {
            return new Contact(dr);
        }

        private Contact()
        {
            MarkAsChild();
        }

        private Contact(SafeDataReader dr)
        {
            MarkAsChild();
            Fetch(dr);
        }

        #endregion

        #region Data Access

        private void Fetch(SafeDataReader dr)
        {
            _id = dr.GetInt32("Id");
            _companyId = dr.GetInt32("CompanyId");
            _name = dr.GetString("Name");
            _Tel = dr.GetString("Tel");
            _mobile = dr.GetString("Mobile");
            _position = dr.GetString("Position");
            _QQ = dr.GetString("QQ");
            _MSN = dr.GetString("MSN");
            _description = dr.GetString("Description");

            MarkOld();
        }

        internal void Insert(int parentId, CompanyType category)
        {
            _companyId = parentId;
            NBearLite.InsertSqlSection insert =
                DataService.DBService.Insert(Tables.Contacts)
                .AddColumn(Tables.Contacts.CompanyId, _companyId)
                .AddColumn(Tables.Contacts.CompanyCategory, category)
                .AddColumn(Tables.Contacts.Name, _name)
                .AddColumn(Tables.Contacts.Tel, _Tel)
                .AddColumn(Tables.Contacts.Mobile, _mobile)
                .AddColumn(Tables.Contacts.Position, _position)
                .AddColumn(Tables.Contacts.QQ, _QQ)
                .AddColumn(Tables.Contacts.MSN, _MSN)
                .AddColumn(Tables.Contacts.Description, _description);
            _id = insert.ExecuteReturnAutoIncrementID(Tables.Products.Id);
            MarkOld();
        }

        internal void Update(int parentId, CompanyType category)
        {
            _companyId = parentId;
            NBearLite.UpdateSqlSection update =
                DataService.DBService.Update(Tables.Contacts)
                .AddColumn(Tables.Contacts.CompanyId, _companyId)
                .AddColumn(Tables.Contacts.CompanyCategory, category)
                .AddColumn(Tables.Contacts.Name, _name)
                .AddColumn(Tables.Contacts.Tel, _Tel)
                .AddColumn(Tables.Contacts.Mobile, _mobile)
                .AddColumn(Tables.Contacts.Position, _position)
                .AddColumn(Tables.Contacts.QQ, _QQ)
                .AddColumn(Tables.Contacts.MSN, _MSN)
                .AddColumn(Tables.Contacts.Description, _description)
                .Where(Tables.Contacts.Id == _id);
            update.Execute();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            DataService.DBService.Delete(Tables.Contacts)
                .Where(Tables.Contacts.Id == _id).Execute();
            MarkNew();
        }

        #endregion
    }
}
